<?php namespace App\Http\Controllers\Site;

use App\Http\Controllers\Controller;
use App\Item;

class IndexController extends Controller
{
    public function getIndex()
    {
        $news = Item::where('type','news')->where('kind','!=',2)->orderBy('created_at','DESC')->take(4)->get();
        $newsPics = Item::where('type','news')->where('kind',2)->orderBy('created_at','DESC')->take(4)->get();
        $companies = Item::where('type','company')->orderBy('created_at','DESC')->take(12)->get();
        return view('site.index',[
            'news' => $news,
            'newsPics' => $newsPics,
            'companies' => $companies,
            'pageName' => 'index',
        ]);
    }

    public function getAbout($id = ""){
        if("" == $id){
            $model = Item::where('type','about')->first();
        } else {
            $model = Item::find($id);
        }
        if(!isset($model->type) || 'about' != $model->type){
            abort(404);
        }
        return view('site.about.index',[
            'model' => $model,
            'pageName' => 'about',
        ]);
    }

    public function getService($type = ""){
        $services = Item::where('type','service');
        if("" != $type){
            $services = $services->where('kind',$type);
        }
        $services = $services->get();
        return view('site.service',[
            'services' => $services,
            'type' => $type,
            'pageName' => 'service',
        ]);
    }

    public function getFacility($id = ""){
        if("" == $id){
            $model = Item::where('type','facility')->first();
        } else {
            $model = Item::find($id);
        }
        if(!isset($model->type) || 'facility' != $model->type){
            abort(404);
        }
        return view('site.facility.index',[
            'model' => $model,
            'pageName' => 'facility',
        ]);
    }

    public function getContact(){
        $contacts = Item::where('type','contact')->get();
        return view('site.contact',[
            'contacts' => $contacts,
            'pageName' => 'contact',
        ]);
    }

    public function getMap(){
        return view('site.map');
    }

    public function getTest()
    {
        $items = config('site.items');
        foreach ($items as $itemName => $item) {
            if (2 == $item['nav']) {
                continue;
            }
            $navs[$itemName]['nav'] = $item['nav'];
            $navs[$itemName]['name'] = $item['name'];
            $navs[$itemName]['itemName'] = $itemName;
            $navs[$itemName]['nav'] = $item['nav'];

            if (1 == $item['nav']) {
                if (isset($item['type'])) {
                    if ($item['data'][$item['type']]['type'] == "dynamic") {
                        $navs[$itemName]['list'] = Item::where('type', $item['data'][$item['type']]['value'])->lists('name', 'id');
                    } else {
                        $navs[$itemName]['list'] = $item['data'][$item['type']]['value'];
                    }
                } else {
                    $navs[$itemName]['list'] = Item::where('type', $itemName)->lists('name', 'id');
                }
            }
        }
        dd($navs);
    }
}